//
//  KKMessageItem+UITableViewCell.m
//  KKLiveDemo
//
//  Created by leo on 2017/7/30.
//  Copyright © 2017年 leo. All rights reserved.
//

#import "KKMessageItem+UITableViewCell.h"
#import "KKLiveHelper.h"
#import "KKLiveMessageCells.h"
#import <objc/runtime.h>

static CGFloat KKMessageItem_Content_DefaultHeight = 30.0;
//static CGFloat KKMessageItem_Content_Max

@implementation KKMessageItem (UITableViewCell)

@dynamic cellHeight;
static const void *KKMessageItem_CellHeightValue = &KKMessageItem_CellHeightValue;
-(void)setCellHeight:(CGFloat)cellHeight {
    objc_setAssociatedObject(self, KKMessageItem_CellHeightValue, @(cellHeight), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CGFloat)cellHeight {
    return [objc_getAssociatedObject(self, KKMessageItem_CellHeightValue) floatValue];
}

@dynamic cellContentSize;
static const void *KKMessageItem_CellContentSizeValue = &KKMessageItem_CellContentSizeValue;
- (void)setCellContentSize:(CGSize)cellContentSize {
    objc_setAssociatedObject(self, KKMessageItem_CellContentSizeValue, [NSValue valueWithCGSize: cellContentSize], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CGSize)cellContentSize {
//    return KKMessageItem_Content_DefaultHeight;
    return [objc_getAssociatedObject(self, KKMessageItem_CellContentSizeValue) CGSizeValue];
}

//  获取cell
- (KKLiveMessageBaseTableViewCell*)tableView:(UITableView*)tableView style:(KKLiveMessageBaseTableViewCellStyle)style {
    NSString *reuseId = [self msgCellReuseIndentifier];
    KKLiveMessageBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: reuseId];
    if (cell == nil) {
        if (style == KKLiveMessageBaseTableViewCellStyle_C2C) {
            cell = [[[self showCellClass] alloc] initWithC2CReuseIdentifier: reuseId];
        }
        else {
            cell = [[[self showCellClass] alloc] initWithGroupReuseIdentifier: reuseId];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

//  获取cell的高度
- (CGFloat)heightInWidth:(CGFloat)width style:(KKLiveMessageBaseTableViewCellStyle)style {
    
    //  如果cell的Height不为0，则显示height值，否则计算height的值，并且存储起来
    if (self.cellHeight != 0.0) {
        return self.cellHeight;
    }
    
    if (self.type == KKMessageType_TimeTip) {
        return 20.0;
    }
    else {
        
        //  内容区域
        CGSize contentSize = [self contentSizeInWidth: width];
        
        CGFloat spaceHeight = 10.0;
        if (style == KKLiveMessageBaseTableViewCellStyle_Group) {
            //  显示群成员昵称，及其其他的信息
            spaceHeight += 30;
        }
        self.cellHeight = contentSize.height + spaceHeight;
        return self.cellHeight;
    }
}

// 只算内容的size
- (CGSize)contentSizeInWidth:(CGFloat)width {
    
    if (self.cellContentSize.width != 0) {
        return self.cellContentSize;
    }
    NSInteger maxWidth = KKLIVE_SCREEN_WIDTH * 0.6;
    if (width > maxWidth) {
        width = maxWidth;
    }
    CGFloat h = 0;
    CGFloat w = 0;
    
    self.cellContentSize = CGSizeMake(w, h);
    return self.cellContentSize;
}

// 带背景图的Size
- (CGSize)contentBackSizeInWidth:(CGFloat)width {
    return CGSizeMake(width * 0.6, KKMessageItem_Content_DefaultHeight + 10);
}

#pragma mark - Private methods
- (NSString*)msgCellReuseIndentifier {
    return @"";
}

- (Class)showCellClass {
    switch (self.type) {
        case KKMessageType_Text:
            return [KKLiveTextMessageCell class];
            break;
        case KKMessageType_Face:
            return [KKLiveEmoticonMessageCell class];
            break;
        case KKMessageType_Image:
            return [KKLivePictureMessageCell class];
            break;
        case KKMessageType_Sound:
            return [KKLiveAudioMessageCell class];
            break;
        case KKMessageType_TimeTip:
            return [KKLiveTimeTipsMessageCell class];
            break;
        default:
            return [KKLiveMessageBaseTableViewCell class];
            break;
    }
}

@end
